package xiangmu.schoolpaper.config;

import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import xiangmu.schoolpaper.interceptor.JwtTokenInterceptor;

/**
 * 配置类，注册web层相关组件
 */
@Configuration
@Slf4j
public class WebMvcConfiguration implements WebMvcConfigurer {

    // 自定义的拦截器对象
    @Autowired
    private JwtTokenInterceptor jwtTokenInterceptor;

    /**
     * TODO 仍需解决 "/favicon.ico"拦截问题
     * TODO "/.well-known/appspecific/com.chrome.devtools.json"解决了上述"/favicon.ico"报401错误的问题，但是仍不知道错误原理
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(jwtTokenInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(
                        "/favicon.ico",           // Favicon 请求
                        "/login",                 // 登录接口
                        "/doc.html",              // Swagger 文档页面
                        "/swagger-ui.html",       // Swagger UI 页面
                        "/swagger-ui/**",         // Swagger UI 静态资源
                        "/v3/api-docs/**",        // Swagger API 文档
                        "/v3/api-docs/default",   // 默认 Swagger API 文档
                        "/swagger-resources/**",  // Swagger 资源
                        "/webjars/**",            // WebJars 资源
                        "/error",
                        "/.well-known/appspecific/com.chrome.devtools.json"
//                        "/static/**",             // 所有静态资源
//                        "/css/**",                // CSS 文件
//                        "/js/**",                 // JS 文件
//                        "/images/**"              // 图片文件
                );
    }

//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/"); // 为 doc.html 映射资源路径
//        registry.addResourceHandler("/swagger-ui/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); // Swagger UI 资源
//        registry.addResourceHandler("/favicon.ico/**").addResourceLocations("classpath:/static/"); // Favicon
//    }

    /**
     * 配置接口文档
     */
    @Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI()
                .info(new Info()
                        .title("档案柜系统接口文档")
                        .version("v1.0")
                        .description("档案柜系统接口文档"));
    }

    @Bean
    public GroupedOpenApi docket1() {
        return GroupedOpenApi.builder()
                .group("接口")
                .pathsToMatch("/**")
                .packagesToScan("xiangmu.schoolpaper.controller")
                .build();
    }

    /**
     * 设置静态资源映射，适配SpringDoc OpenAPI
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 配置Swagger UI静态资源
        registry.addResourceHandler("/swagger-ui/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/springdoc-openapi-ui/");
        // 配置API文档资源
        registry.addResourceHandler("/v3/api-docs/**")
                .addResourceLocations("classpath:/META-INF/resources/");
    }
}
